/*
 * @Author: szx
 * @Date: 2022-02-14 13:20:15
 * @LastEditTime: 2022-02-14 14:01:05
 * @Description:
 * @FilePath: \leetcode\1700-1799\1765\1765.js
 */
/**
 * @param {number[][]} isWater
 * @return {number[][]}
 */
var highestPeak = function (isWater) {
    const m = isWater.length;
    const n = isWater[0].length;

    const dirs = [
        [-1, 0],
        [1, 0],
        [0, -1],
        [0, 1]
    ];
    const res = new Array(m).fill(0).map(() => new Array(n).fill(-1));
    let queue = [];
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (isWater[i][j]) {
                res[i][j] = 0;
                queue.push([i, j]);
            }
        }
    }
    while (queue.length) {
        const length = queue.length,
            tmp_q = [];
        for (let i = 0; i < length; i++) {
            const p = queue[i];
            for (let dir of dirs) {
                const row = p[0] + dir[0];
                const col = p[1] + dir[1];
                if (row >= 0 && row < m && col >= 0 && col < n && res[row][col] == -1) {
                    res[row][col] = res[p[0]][p[1]] + 1;
                    tmp_q.push([row, col]);
                }
            }
        }
        queue = tmp_q;
    }

    return res;
};
console.log(
    highestPeak([
        [0, 0, 1],
        [1, 0, 0],
        [0, 0, 0]
    ])
);
